'use strict';

exports.__esModule = true;
exports.Root = undefined;

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _reactRedux = require('react-redux');

var _modal = require('antd/lib/modal');

var _modal2 = _interopRequireDefault(_modal);

var _system = require('./modules/system');

var actions = _interopRequireWildcard(_system);

var _ErrorModal = require('./components/ErrorModal');

var _ErrorModal2 = _interopRequireDefault(_ErrorModal);

var _utils = require('./libs/utils');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Root = exports.Root = function (_PureComponent) {
  (0, _inherits3.default)(Root, _PureComponent);

  function Root() {
    (0, _classCallCheck3.default)(this, Root);
    return (0, _possibleConstructorReturn3.default)(this, _PureComponent.apply(this, arguments));
  }

  Root.prototype.componentDidMount = function componentDidMount() {
    document.body.classList.add('ProximaNova--loaded');
  };

  Root.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
    var system = nextProps.system;
    var errorAlert = system.errorAlert;

    var originalErrorAlert = this.props.system.errorAlert;

    if (originalErrorAlert !== errorAlert) {
      if (this.currentErrorAlert) {
        this.currentErrorAlert.destroy();
        this.currentErrorAlert = null;
      }

      if (errorAlert) {
        this.currentErrorAlert = _modal2.default.error({
          content: errorAlert,
          onCancel: this.props.errorAlertHide
        });
        this.props.errorAlert(errorAlert);
      }
    }
  };

  Root.prototype.render = function render() {
    var _props = this.props,
        store = _props.store,
        view = _props.view,
        system = _props.system,
        errorModalHide = _props.errorModalHide,
        modalHide = _props.modalHide;
    var errorModal = system.errorModal,
        modal = system.modal;


    var ModalView = void 0;
    var ModalViewData = void 0;
    if (modal) {
      ModalView = modal.view;
      ModalViewData = modal.data;
    }

    return _react2.default.createElement(
      'div',
      null,
      view(store),
      errorModal && _react2.default.createElement(_ErrorModal2.default, { error: errorModal, onClose: errorModalHide }),
      modal && _react2.default.createElement(ModalView, (0, _extends3.default)({ onClose: modalHide }, ModalViewData))
    );
  };

  return Root;
}(_react.PureComponent);

Root.propTypes = {
  system: _propTypes.object, // eslint-disable-line
  store: _propTypes.object, // eslint-disable-line
  view: _propTypes.func,
  errorModalHide: _propTypes.func,
  errorAlert: _propTypes.func,
  errorAlertHide: _propTypes.func,
  modalHide: _propTypes.func
};
Root.defaultProps = {
  system: {},
  view: _utils.identity
};


var mapStateToProps = function mapStateToProps(state) {
  return {
    system: state.system
  };
};

exports.default = (0, _reactRedux.connect)(mapStateToProps, actions)(Root);